<?php
namespace Admin\Controller;
// 卡号管理
use Common\Api\SystemApi;
use Common\Api\UserApi;
class CardController extends CommonController {
	
   /* public function index(){
       $yunying = new Yunying();
       $list = $yunying->select();
       echo 666;die();
       dump($list);die();
        $this->display();
    }*/
    public function outxls(){
	   $filename='卡片列表';
       $map=$this->_search();
       
       if($_REQUEST ['ids']!='all'){
       	 $map['id'] = array('in', explode(',', $_REQUEST ['ids']));
       }
        $volist = D('Card')->where($map)->field('id,card_id,daili_id,yunying_id,taocan_id,status,create_time')->select();
       foreach ($volist as $key =>$vo){
           /*运营商*/
           $yunying = D('Yunying')->find($vo['yunying_id']);
           $volist[$key]['yunying_id']= $yunying['name'];
           /*套餐*/
           $taocan = D('Taocan')->find($vo['taocan_id']);
           $volist[$key]['taocan_id']= $taocan['name'];
			$volist[$key]['create_time']=date("Y-m-d",$vo['create_time']);
		}
		
		$headArr=array("id","卡号","代理id","运营商","套餐","状态","创建日期");
    
		$this->xlsout($filename,$headArr,$volist);
        
		
    }

public function rolelist(){
	
	    $role = D("Role");

        $map=$this->_search('Role');
	    
	   
	    $map['status']=1;
	   
		$this->_list($role,$map);
		$this->display();
	
}
	public function add(){
		$admin_id = $_SESSION['zs_admin']['userid'];
		if ($admin_id == 1) {
			$dengji = 2;
		}else{
			$user = M('user')->where(array('id'=>$admin_id))->find();//当前用户
			if ($user['dengji'] == 2) {
				$dengji = 3;
			}
			if ($user['dengji'] == 3) {
				$dengji = 4;
			}
		}

		$this->assign('dengji',$dengji);
		$this->assign('admin_id',$admin_id);
		$this->display();
	}
	function edit() {
        $model = M('Card');
		$id = $_REQUEST [$model->getPk()];
		$vo = $model->getById($id);
		$this->assign('vo', $vo);
		$this->display();
	}

    public function RequestHttp($url){
       /* $url = "http://api.e114.xin/m2m_ec/query.do?user_id=miaoyue&access_number=1410331155200&sign=69261F8F5AA3C91B0B6806612018532C646B079B68A86AC0C1F669AA1AB5CD61D54FC8590DB4FBE5A052D333E0881BD2014278DB619743B89191E7314D0515529DA4295AB49E3D744E7C805C1B980BD9E4B6307D6DA31204704763269F2AC6B0A5DD546D5360A2642118568CDC4C5F3B&passWord=A301AFB6BF63307F3C1B159174BA6F47B829A2BF80005B50AB7FF0A286F73444&method=disabledNumber&orderTypeId=20";*/

        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
        $tmpInfo = curl_exec($curl);     //返回api的json对象
        //关闭URL请求
        curl_close($curl);
        return $tmpInfo;    //返回json对象
    }
    /*开机,关机接口测试*/
    public function telecom_bootup($access_number,$yunying_name,$jiekou_name)
    {
        $yunying = M('yunying');
        $data = $yunying->where(array('name' => $yunying_name))->find();
        /*获取接口地址*/
        $jiekou = M('jiekou');
        $url = $jiekou->where(array('yunying_id' => $data['id'],'name' => '开机'))->getField('content');
        if($yunying_name == '电信'){
            /*电信*/
            header("Content-type:text/html;charset=utf-8");
            $user_id=$data['username'];		//用户名
            $password=$data['password'];//密码
            if($jiekou_name == 'open'){
                $method = "singleCutNet";//开机
            }
            if($jiekou_name == 'close'){
                $method = "disabledNumber";  //关机
            }
            $orderTypeId = 20;
            $arr=array($access_number,$user_id,$password,$method,$orderTypeId);//加密数组，数组所需参数根据对应的接口文档
            $key1 = $data['key1'];
            $key2 = $data['key2'];
            $key3 = $data['key3'];
            $des = new SystemApi();
           // echo 65656;die();
           // $des=new DesUtils(); //加密工具类实例化
            $arrOrder=$des->naturalOrdering($arr); //加密数组元素排序
            $sign = $des->strEnc($arrOrder,$key1,$key2,$key3);
            $passwords = $des->strEnc($password,$key1,$key2,$key3);
            $urls = $url."?user_id=".$user_id."&access_number=".$access_number."&sign=".$sign."&passWord=".$passwords."&method=".$method."&orderTypeId=".$orderTypeId."";

            $xml = $this->RequestHttp($urls);
            dump($xml);die();
            print_r($xml);exit;
            $reader = new XMLReader(); //实例化XMLReader
            $test = $reader->open($urls); //获取xml

            $i=1;
            while ($reader->read()) {
                if ($reader->nodeType == XMLReader::TEXT) { //判断node类型
                    $m = $i%3;
                    $arr[] = $reader->value;
                    $i++;
                }
            }

            if ($Arr[0] == 0) {
                //开机成功
                $where['device_sn'] = $access_number;
                $res['state'] = 1;
                $this->Device_m->update($where,$res);
                return array('code'=>1,'msg'=>'开机成功','access_number'=>$access_number);
            }else{
                return array('code'=>2,'msg'=>'开机失败','access_number'=>$access_number);
            }
        }


    }

    /*断网,取消断网*/

    public function internet_choose($access_number,$yunying_name,$jiekou_name){
        $yunying = M('yunying');
        $data = $yunying->where(array('name' => $yunying_name))->find();
        /*获取接口地址*/
        $jiekou = M('jiekou');
        $url = $jiekou->where(array('yunying_id' => $data['id'],'name' => '断网'))->getField('content');
        if($yunying_name == '电信'){
        set_time_limit(0);
        header("Content-type:text/html;charset=utf-8");
        $user_id=$data['username'];		//用户名
        $password=$data['password'];//密码
        $method = "singleCutNet";  //接口名-套餐使用量查询 ADD DEL
        $orderTypeId = $jiekou_name;
        $arr=array($access_number,$user_id,$password,$method,$orderTypeId);//加密数组，数组所需参数根据对应的接口文档
        $key1 = $data['key1'];
        $key2 = $data['key2'];
        $key3 = $data['key3'];
        $des = new SystemApi(); //加密工具类实例化

        $arrOrder=$des->naturalOrdering($arr); //加密数组元素排序
        $sign = $des->strEnc($arrOrder,$key1,$key2,$key3);
        $passwords = $des->strEnc($password,$key1,$key2,$key3);
        $urls = $url."?user_id=".$user_id."&access_number=".$access_number."&sign=".$sign."&passWord=".$passwords."&method=".$method."&action=".$orderTypeId."";
        $xml = $this->RequestHttp($urls);
       /* if(is_string($xml)){
           echo $xml;die();
            }*/
       $res = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        if(!$res){
            echo "操作失败,稍后再试".$xml;die();
        }
        $card = M('card');
        if($jiekou_name == 'DEL'){
            $card_datas['status'] = '3';
            $res =  $card->where('card_id='.$access_number)->save($card_datas);
            echo "断网成功";die();
        }
        if($jiekou_name == 'ADD'){
            $card_datas['status'] = '4';
            $res =  $card->where('card_id='.$access_number)->save($card_datas);
            echo "取消断网成功";die();
        }

    }
    }
    /*查询*/
    public function see($access_number,$yunying_name)
    {
        $yunying = M('yunying');
        $data = $yunying->where(array('name' => $yunying_name))->find();
        /*获取接口地址*/
        $jiekou = M('jiekou');
        $url = $jiekou->where(array('yunying_id' => $data['id'],'name' => '查询'))->getField('content');
        if($yunying_name == '电信'){
            set_time_limit(0);
            header("Content-type:text/html;charset=utf-8");
            $user_id=$data['username'];		//用户名
            $password=$data['password'];//密码
            $method = "queryPakage";  //接口名-套餐使用量查询 ADD DEL
            $arr=array($access_number,$user_id,$password,$method);//加密数组，数组所需参数根据对应的接口文档
            $key1 = $data['key1'];
            $key2 = $data['key2'];
            $key3 = $data['key3'];
            $des = new SystemApi(); //加密工具类实例化

            $arrOrder = $des->naturalOrdering($arr); //加密数组元素排序
            $sign = $des->strEnc($arrOrder, $key1, $key2, $key3);
            $passwords = $des->strEnc($password, $key1, $key2, $key3);
            $time = time();
            $urls = $url."?user_id=" . $user_id . "&access_number=" . $access_number . "&sign=" . $sign . "&passWord=" . $passwords . "&method=queryPakage";
            $xml = $this->RequestHttp($urls);
            dump($xml);die();
            $xmlTag = array(
                'ACCU_NAME',
                'CUMULATION_ALREADY',
                'CUMULATION_LEFT'
            );
            $arr = array();
            foreach($xmlTag as $x) {
                preg_match_all("/<".$x.">.*<\/".$x.">/", $xml, $temp);
                $arr[] = $temp[0];
            }
            $data = array();
            foreach($arr as $key => $value) {
                foreach($value as $k => $v) {
                    $a = explode($xmlTag[$key].'>', $v);
                    $v = substr($a[1], 0, strlen($a[1])-2);
                    $data[$k][$xmlTag[$key]] = $v;
                }
            }
            dump($data);die();


        }
    }

    /*移动 开机,关机,断网,恢复断网,断网+恢复断网*/
    public function yd_choose($access_number,$opttype){
        echo '暂不支持';die();
        $yunying = M('yunying');
        $yidong = $yunying->find(2);
        /*接口需要数据*/
        $uname = $yidong['name'];
        $secret = $yidong['password'];
        $tmp = time();
        $card = $access_number;
        $sign = md5($secret);
        $urls = "http://119.23.127.152:8089?uname=".$uname."&secret=".$secret."&tmp=".$tmp."&card=".$card."&sign=".$sign."&opttype=".$opttype."";
        $xml = $this->RequestHttp($urls);
        dump($xml);
    }

    /*查询剩余套餐*/
    public function see_taocan($access_number,$yunying_name)
    {
        $yunying = M('yunying');
        $data = $yunying->where(array('name' => $yunying_name))->find();
        /*获取接口地址*/
        $jiekou = M('jiekou');
        $url = $jiekou->where(array('yunying_id' => $data['id'],'name' => '查询'))->getField('content');
        if($yunying_name == '电信'){
            set_time_limit(0);
            header("Content-type:text/html;charset=utf-8");
            $user_id=$data['username'];		//用户名
            $password=$data['password'];//密码
            $method = "queryPakage";  //接口名-套餐使用量查询 ADD DEL
            $arr=array($access_number,$user_id,$password,$method);//加密数组，数组所需参数根据对应的接口文档
            $key1 = $data['key1'];
            $key2 = $data['key2'];
            $key3 = $data['key3'];
            $des = new SystemApi(); //加密工具类实例化
            $arrOrder = $des->naturalOrdering($arr); //加密数组元素排序
            $sign = $des->strEnc($arrOrder, $key1, $key2, $key3);
            $passwords = $des->strEnc($password, $key1, $key2, $key3);
            $time = time();
            $urls = $url."?user_id=" . $user_id . "&access_number=" . $access_number . "&sign=" . $sign . "&passWord=" . $passwords . "&method=queryPakage";
            $xml = $this->RequestHttp($urls);
            dump($xml);die();
            $xmlTag = array(
                'ACCU_NAME',
                'CUMULATION_ALREADY',
                'CUMULATION_LEFT',
                'CUMULATION_TOTAL',
                'END_TIME',
                'OFFER_ID',
                'OFFER_NAME',
                'START_TIME',
                'UNIT_NAME',
                'OFFER_TYPE',
                'IRESULT',
                'GROUP_TRANSACTIONID',
                'number'
            );
            $arr = array();
            foreach($xmlTag as $x) {
                preg_match_all("/<".$x.">.*<\/".$x.">/", $xml, $temp);
                $arr[] = $temp[0];
            }
            $data = array();
            foreach($arr as $key => $value) {
                foreach($value as $k => $v) {
                    $a = explode($xmlTag[$key].'>', $v);
                    $v = substr($a[1], 0, strlen($a[1])-2);
                    $data[$k][$xmlTag[$key]] = $v;
                }
            }
            dump($data);die();


        }
    }
    /*批量新增*/
    public function cards_insert(){
        $cards = M('card');
        $data['create_time'] = $_POST['create_time'];
        $data['daili_id'] = $_POST['daili_id'];
        $data['yunying_id'] = $_POST['yunying_id'];
        $data['taocan_id'] = $_POST['taocan_id'];
        $arr = explode(",",$_POST['card_ids']);
        foreach ($arr as $v){
            $data['card_id'] = $v;
            $res = $cards->add($data);
        }
        if(!$res){
            $this->mtReturn(400, '批量添加失败！');
        }
        $this->mtReturn(200, '批量添加成功！');
    }

    /*批量转移代理*/
    public function turn_daili(){
        $daili_id = $_GET['daili_id'];
        $ids = $_REQUEST ['ids'];
        $cards = M('card');
        $arr = explode(",",$ids);
        $data['daili_id'] = $daili_id;
        foreach ($arr as $v){
           $res =  $cards->where(array('id'=>$v))->save($data);
        }
        if(!$res){
            $this->mtReturn(400, '批量转移代理失败！',"","",'/index.php?m=Admin&c=Index&a=index#Card');
        }
        $this->mtReturn(200, '批量转移代理成功！',"","",'/index.php?m=Admin&c=Index&a=index#Card');
    }

    /*电信 批量开机,关机,断网,取消断网*/
    public function more_caozuo()
    {

        $status = $_GET['status'];
        $ids = $_REQUEST ['ids'];
        $cards = M('card');
        $arr = explode(",", $ids);
        $yunying = M('yunying');
        $jiekou = M('jiekou');
        $url = $jiekou->where(array('yunying_id' => 4))->getField('content');
        $data = $yunying->where(array('id' => 4))->find();
        foreach ($arr as $v) {
            /*找到卡号*/
                $card_id = $cards->where(array('id'=>$v))->getField('card_id');
            /*获取接口地址*/
                set_time_limit(0);
                header("Content-type:text/html;charset=utf-8");
                $user_id = $data['username'];        //用户名
                $password = $data['password'];//密码
                //开机
                if($status =="singleCutNet"){
                    $method = "singleCutNet";
                    $orderTypeId = '20';
                }
                //关机
                if($status =="disabledNumber"){
                    $method = "disabledNumber";
                    $orderTypeId = '20';
                }
                //断网
                if($status =="DEL"){
                    $method = "singleCutNet";
                    $orderTypeId = 'DEL';
                }
                //取消断网
                if($status =="ADD"){
                    $method = "singleCutNet";
                    $orderTypeId = 'ADD';
                }
                $arr = array($card_id, $user_id, $password, $method, $orderTypeId);//加密数组，数组所需参数根据对应的接口文档
                $key1 = $data['key1'];
                $key2 = $data['key2'];
                $key3 = $data['key3'];
                $des = new SystemApi(); //加密工具类实例化
                $arrOrder = $des->naturalOrdering($arr); //加密数组元素排序
                $sign = $des->strEnc($arrOrder, $key1, $key2, $key3);
                $passwords = $des->strEnc($password, $key1, $key2, $key3);
                if($status == 'ADD' || $status == 'DEL'){
                    $urls = $url . "?user_id=" . $user_id . "&access_number=" . $card_id . "&sign=" . $sign . "&passWord=" . $passwords . "&method=" . $method . "&action=" . $orderTypeId . "";
                }
                if($status == 'singleCutNet' || $status == 'disabledNumber'){
                    $urls = $url."?user_id=".$user_id."&access_number=".$card_id."&sign=".$sign."&passWord=".$passwords."&method=".$method."&orderTypeId=".$orderTypeId."";
                }
                $xml = $this->RequestHttp($urls);
                /* if(is_string($xml)){
                    echo $xml;die();
                     }*/
                if($xml == ""){
                    dump('批量操作失败,请稍后再试');die();
                  /*  $this->mtReturn(400, '批量操作失败,请稍后再试', "", "", '/index.php?m=Admin&c=Index&a=index#Card');*/
                }
                $res = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
                if (!$res) {
                    $this->mtReturn(400, '批量操作失败,请稍后再试', "", "", '/index.php?m=Admin&c=Index&a=index#Card');
                }
                $card = M('card');
                if ($status == 'DEL') {
                    $card_datas['status'] = '3';
                    $res = $card->where('card_id=' . $card_id)->save($card_datas);
                }
                if ($status == 'ADD') {
                    $card_datas['status'] = '4';
                    $res = $card->where('card_id=' . $card_id)->save($card_datas);

                }
                if ($status == 'singleCutNet') {
                    $card_datas['status'] = '1';
                    $res = $card->where('card_id=' . $card_id)->save($card_datas);
                }
                if ($status == 'disabledNumber') {
                    $card_datas['status'] = '2';
                    $res = $card->where('card_id=' . $card_id)->save($card_datas);
                }
            }
            $this->mtReturn(200, '批量操作成功！', "", "", '/index.php?m=Admin&c=Index&a=index#Card');
        }
}

?>